package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 快办清单
 */

public class KuaiBanList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //创建获取详情页信息的对象
        AllInfo allInfo = new AllInfo();

        //请求获取快办清单下每一个模块的请求参数的列表
        String listUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getHeatThemeList?topicCode=16122321233655";
        JSONObject listHeader = new JSONObject();
        listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/fast-manages");
        listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
        JSONObject listParmas = new JSONObject();
        listParmas.put("topicCode", "16122321233655");
        String listStrResponse = getReq(listUrl,listHeader,listParmas);
        JSONObject listResponse = JSONObject.parseObject(listStrResponse);
        //遍历列表获取每个模块的请求参数
        for (Object o : listResponse.getJSONObject("data").getJSONArray("resultList")){
            JSONObject module = (JSONObject) o;
            //请求获取每个模块下每个地区的请求参数列表
            String id = module.getString("id");//acc4dbf90c9a4b3b8bc4a1de11b907ea
            String regionalUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getHighlightArea?topicid="+id;
            JSONObject regionalHeader = new JSONObject();
            regionalHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/fast-manages");
            regionalHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            JSONObject regionalParmas = new JSONObject();
            regionalParmas.put("topicid", id);
            String regionalStrResponse = getReq(regionalUrl,regionalHeader,regionalParmas);
            JSONObject regionalResponse = JSONObject.parseObject(regionalStrResponse);
            //遍历列表获取每个地区的请求参数
            for (Object o1 : regionalResponse.getJSONObject("data").getJSONArray("resultList")){
                JSONObject regional = (JSONObject) o1;
//                System.out.println("regional:\n"+regional);
                if (regional.getString("ishighlight").equals("1")){
                    String region = "海南省-"+regional.getString("deptname");
                    String deptid = regional.getString("deptid");
                    //当前页
                    int currentPage = 1;
                    //总页数
                    int pags = 0;
                    while (currentPage != pags) {
                        String listPageUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getMatterByIdAndCode";
                        JSONObject listPageHeader = new JSONObject();
                        listPageHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/areaList?topicid="+id+"&xzqhid="+deptid);
                        listPageHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                        JSONObject listPageParmas = new JSONObject();
                        listPageParmas.put("topicid", id);
                        listPageParmas.put("xzqhid", deptid);
                        listPageParmas.put("pageNum", currentPage);
                        listPageParmas.put("pageSize", "5");
                        listPageParmas.put("issub", "1");
                        String listPageStrResponse = postFormReq(listPageUrl,listPageHeader,listPageParmas);
                        JSONObject listPageResponse = JSONObject.parseObject(listPageStrResponse);
                        for (Object o2 : listPageResponse.getJSONObject("data").getJSONArray("resultList")) {
                            JSONObject reqData = (JSONObject) o2;
                            String url = "https://wssp.hainan.gov.cn/wssp3/bszn/exempt/allInfo";
                            JSONObject header = new JSONObject();
                            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                            header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id="+reqData.getString("id"));
                            JSONObject parmas = new JSONObject();
                            parmas.put("rowguid", reqData.getString("rowguid"));//46f219bb410a42ef8c0163c2c92453bd
                            parmas.put("id", reqData.getString("sourcekey"));//24642C5D36CF430EBAE624A9A242880B
                            parmas.put("type", "1");
                            allInfo.setAllInfoUrl(url);
                            allInfo.setAllInfoHeader(header);
                            allInfo.setAllInfoParmas(parmas);
                            JSONObject result;
                            try {
                                result = allInfo.getAllInfo();
                            } catch (Exception e) {
                                break;
                            }
                            System.out.println(currentPage+"\n"+region+"\n"+result);//测试用代码
                            return processResult(
                                    null,
                                    null,
                                    null,
                                    allInfo.getResponce(),
                                    result,
                                    region,
                                    "政务公开-快办清单"
                            );
                        }
                        //更新当前页
                        currentPage++;
                        //获取总页数
                        pags = listPageResponse.getJSONObject("data").getJSONObject("pageMap").getInteger("pages") + 1;
                    }
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        KuaiBanList k = new KuaiBanList();
        k.getDataDemo();
    }
}
